// pages/recruit/function/function.js
import WeCropper from '../../../we-cropper/we-cropper.js';

const device = wx.getSystemInfoSync();
const width = device.windowWidth;
const height = device.windowHeight;

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 基本参数
    companyName_text: '',
    phone_text: '',
    companyIntr_text: '',
    companyIntr_num: '',
    companyScale_text: '',
    anchorScale_text: '',
    logo_url: '',
    scaleArray: ['50人以下','50-100人','100-500人','500人以上'],
    scaleIndex: 0,

    shimmer_text: '',
    detailed_text: '',
    detailed_num: '',

    //类型状态
    status_type: 1,
    success_pic:false,

    // 显示按钮 Start
    company_flag: false,
    job_flag: false,
    discover:false,
    tailor_show:true,
    focus:false,
    // 显示按钮 End

    //裁剪后的图片
    img_url: '',
    img_base64: '',

    // 裁剪图片
    cropperOpt: {
      id: 'cropper',
      width,
      height,
      scale: 2.5,
      zoom: 8,
      cut: {
        x: (width - 210) / 2,
        y: (height - 165) / 2,
        width: 210,
        height: 165
      }
    },
  },
  //获得焦点
  click_focus(){
    this.setData({
      focus:true,
    })
  },
  //提交logo
  click_logo(){
    let that=this;
    that.setData({
      tailor_show:false,
    })
  },
  //提交职位介绍
  submit_job() {
    if (this.data.discover){
      //回到找主播
      wx.redirectTo({
        url: '../discoverAnchor/discoverAnchor',
      })
    }else{
      //回到发布职位
      const pages = getCurrentPages();
      const currPage = pages[pages.length - 1]; //当前页面
      const prevPage = pages[pages.length - 2]; //上一个页面

      wx.navigateBack({
        delta: 1,
      });

      // 设置上一个页面值
      prevPage.setData({
        detailed_text: this.data.detailed_text,
        // lightspot: this.data.shimmer_text,
      })
    }
    
  },

  //提交公司信息
  submit_info() {
    let that = this;
    const pages = getCurrentPages();
    const currPage = pages[pages.length - 1]; //当前页面
    const prevPage = pages[pages.length - 2]; //上一个页面

    if (this.data.companyName_text && this.data.phone_text && this.data.companyIntr_text && this.data.companyScale_text && this.data.success_pic && this.data.anchorScale_text) {
      console.info(this.data.companyScale_text)
      console.info(this.data.anchorScale_text)
      wx.request({
        url: app.globalData.url + 'api/guild/company/add',
        data: {
          token: app.globalData.token,
          type: this.data.status_type,
          company_name: this.data.companyName_text,
          telephone: this.data.phone_text,
          company_description: this.data.companyIntr_text,
          company_scale: this.data.companyScale_text,
          logo: this.data.img_base64,
          anchor_scale: this.data.anchorScale_text,
        },
        method: 'POST',
        success: function(e) {
          console.log(e)
          //同步执行
          wx.navigateBack({
            delta: 1,
          });
          // 设置上一个页面值
          prevPage.setData({
            companyName_text: that.data.companyName_text
          })
        },
      })
    } else {
      wx.showToast({
        title: '请填写完整所有信息~',
        icon: 'none'
      })
    }


  },
  //详细描述
  inp_detailed(e) {
    let that = this;
    // console.log(e.detail)
    that.setData({
      detailed_text: e.detail.value,
      detailed_num: e.detail.value.length,

    })
  },
  //职位亮点
  inp_shimmer(e) {
    let that = this;
    // console.log(e.detail)
    that.setData({
      shimmer_text: e.detail.value,
    })
  },

  //公司介绍
  inp_companyIntr(e) {
    let that = this;
    // console.log(e.detail)
    that.setData({
      companyIntr_text: e.detail.value,
      companyIntr_num: e.detail.value.length,
    })
  },
  //电话
  inp_phone(e) {
    let that = this;
    // console.log(e.detail.value)
    that.setData({
      phone_text: e.detail.value,
    })
  },
  //公司名称
  inp_companyName(e) {
    let that = this;
    console.log(e.detail.value)
    that.setData({
      companyName_text: e.detail.value,
    })
  },

  /**旗下主播 */
  inp_anchorScale(e) {
    let that = this;
    console.log(e.detail.value)
    that.setData({
      anchorScale_text: e.detail.value,
    })
  },

  //获得原始公司名称等数据
  get_companyDetail(){
    let that=this;
    wx.request({
      url: app.globalData.url + 'api/guild/company/add',
      data: {
        token:app.globalData.token,
        type:2,
      },
      method: 'POST',
      success: function(e) {
        // console.log(e)
        if (!e.data.data.company.company_name){

        }else{
          /**获取公司规模滑动索引 */
          let has_scale = true;
          let scale_index = that.data.scaleArray.indexOf(e.data.data.company.company_scale);
          if(scale_index < 0){
            has_scale = false;
          }
          if (e.data.data.company.company_name=="未设置"){
            var companyName_text="";
          }else{
            var companyName_text = e.data.data.company.company_name;
          }
          that.setData({
            companyName_text: companyName_text,
            phone_text: e.data.data.company.telephone,
            companyIntr_text: e.data.data.company.company_description,
            scaleIndex: scale_index,
            logo_url: e.data.data.company.logo,
            companyScale_text: has_scale?that.data.scaleArray[scale_index]:'',
            anchorScale_text: e.data.data.company.anchor_scale,
            success_pic:true,
          })
        }
        
      },
    })
  },
  //选择 裁剪图片
  click_close: function () {
    this.setData({
      tailor_show: true,
    })
  },
  touchStart(e) {
    this.wecropper.touchStart(e)
  },
  touchMove(e) {
    this.wecropper.touchMove(e)
  },
  touchEnd(e) {
    this.wecropper.touchEnd(e)
  },
  getCropperImage() {
    wx.showLoading({
      title: '加载中~',
      icon: "none",
    })
    let that = this;
    this.wecropper.getCropperImage((src) => {
      if (src) {
        // console.log('图片+'+src)
        var image_base64 = wx.getFileSystemManager().readFileSync(src, "base64");
        // //发送请求
        // wx.request({
        //   url: app.globalData.url + 'api/mine/upload/avatar',
        //   method: "POST",
        //   header: {
        //     "content-type": "application/x-www-form-urlencoded"
        //   },
        //   data: {
        //     token: app.globalData.token,
        //     image: image_base64 + '',
        //   },
        //   success: function (e) {
        //     // console.log(e.data.data.file_url)
        //     let file_url = e.data.data.file_url;
        //     wx.setStorageSync("reg_avatar_url", file_url)
        //   }
        // })
        console.log(src)
        that.setData({
          img_url: src,
          img_base64: image_base64 + '',
          tailor_show: true,
          success_pic:true,
        })

        wx.hideLoading();

      } else {
        console.log('获取图片地址失败，请稍后重试')
      }

    });
  },
  uploadTap() {
    const self = this;

    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success(res) {
        const src = res.tempFilePaths[0]
        //  获取裁剪图片资源后，给data添加src属性及其值
        self.wecropper.pushOrign(src)
      }
    })
  },
  //裁剪图片 初始化
  init: function () {
    const {
      cropperOpt
    } = this.data

    new WeCropper(cropperOpt)
      .on('ready', (ctx) => {
        console.log(`wecropper is ready for work!`)
      })
      .on('beforeImageLoad', (ctx) => {
        console.log(`before picture loaded, i can do something`)
        console.log(`current canvas context:`, ctx)
        wx.showToast({
          title: '上传中',
          icon: 'loading',
          duration: 20000
        })
      })
      .on('imageLoad', (ctx) => {
        console.log(`picture loaded`)
        console.log(`current canvas context:`, ctx)
        wx.hideToast()
      })
      .on('beforeDraw', (ctx, instance) => {
        console.log(`before canvas draw,i can do something`)
        console.log(`current canvas context:`, ctx)
      })
      .updateCanvas()
  },

  /**
   * 公司规模更新
   */
  bindScalePickerChange(e) {
    let that = this;
    let scale_index = e.detail.value;
    that.setData({
      companyScale_text: that.data.scaleArray[scale_index],
      scaleIndex: scale_index
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    this.init();
    let that = this;
    if (options.discover){
      that.setData({
        discover:true
      })
    }
    if (options.flag) {
      var flag = options.flag;
      // 分布显示
      switch (flag) {
        case "company_flag":
          that.setData({
            company_flag: true,
          })
          break;
        case "job_flag":
          that.setData({
            job_flag: true,
          })
          break;
        default:
          console.log("null")
      }
    };
    this.get_companyDetail();




  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})